{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 把数字转化为字符  \n",
    "+ 第一个数字表示这张图片表示哪个数，后面是图片数据  \n",
    "+ 每个数字之间用逗号隔开  \n",
    "+ 追加到文件后面  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def savedata(a,number,filename):\n",
    "    b=a.reshape(-1)\n",
    "    b=map(str,b)\n",
    "    b=list(b)\n",
    "    b=str(number)+','+','.join(b)+'\\n'   \n",
    "    f=open(filename,mode='a')\n",
    "    f.writelines(b)\n",
    "    f.close()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 图像增强，增强对比度\n",
    "+ 如果增强效果不好，140可以修改为其他数字，一般在120~180之间\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reinforce(n):\n",
    "    if n>140:\n",
    "        return 255\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x237ef8ae978>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAACzpJREFUeJzt3U+InPUdx/HPp1Ev6iGSSQgx6VoJpVJo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9Wi3edwPm+pVauN/a2PNvlvRxRHwSEV9JekXS9hbmmHgRcVjS2Ys2b5e0rzi/T/P/8zRuidkmQkTMRsQHxflzki6sLN3qfTdgrla0Ef86SZ8tuHxSk7Xkd0h6y/b7tqfbHmYRa4pl0y8sn7665XkuNnTl5iZdtLL0xNx346x4XbU24l9s9Z9JOuSwJSJ+KulOSQ8VD28xmpFWbm7KIitLT4RxV7yuWhvxn5S0fsHlGyWdamGORUXEqeLrnKQ3NHmrD5++sEhq8XWu5Xm+NUkrNy+2srQm4L6bpBWv24j/PUkbbd9k+xpJ90na38Icl7B9bfGLGNm+VtIdmrzVh/dL2lmc3ynpzRZn+Y5JWbl5qZWl1fJ9N2krXrfyIp/iUMYfJK2QtDciftv4EIuw/QPN7+2l+UVMX2pzNtsvS9qq+Xd9nZb0hKS/SnpV0gZJn0q6NyIa/8XbErNt1fxD129Xbr7wHLvh2X4u6R+Sjkr6ptj8uOafX7d23w2Ya4dauN94hR+QFK/wA5IifiAp4geSIn4gKeIHkiJ+ICniB5IifiCp/wO2leJkrOPKLgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n=5\n",
    "img=Image.open(str(n)+\".jpg\")\n",
    "img = img.resize((28,28),Image.ANTIALIAS)\n",
    "a=np.asarray(img)\n",
    "a=a[:,:,0]\n",
    "a=a.reshape(-1)\n",
    "a=map(reinforce,a)\n",
    "a=np.array(list(a))\n",
    "a=a.reshape([28,28])\n",
    "savedata(a,n,\"data.txt\")\n",
    "\n",
    "im = Image.fromarray(np.uint8(a))\n",
    "plt.imshow(im)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
